package metric

import (
	"gddgame.cc/galaxy/binocle"
	"github.com/toolkits/nux"
)

func If(agent binocle.Service) error {
	if _, err := nux.NetIfs([]string{}); err != nil {
		return err
	}

	agent.AddMetrics().Collect(func() map[string]float32 {
		netIfs, _ := nux.NetIfs([]string{})
		var inBytes int64 = 0
		var inPackages int64 = 0
		var outBytes int64 = 0
		var outPackages int64 = 0
		var totalBytes int64 = 0
		var totalPackages int64 = 0
		var speed int64 = 0
		for _, netIf := range netIfs {
			inBytes += netIf.InBytes
			inPackages += netIf.InPackages
			outBytes += netIf.OutBytes
			outPackages += netIf.OutPackages
			totalBytes += netIf.TotalBytes
			totalPackages += netIf.TotalPackages
			inBytes += netIf.InBytes
			speed += netIf.SpeedBits
		}
		return map[string]float32{
			"net.if.bytes":          float32(inBytes),
			"net.if.packets":        float32(inPackages),
			"net.if.out.bytes":      float32(outBytes),
			"net.if.out.packages":   float32(outPackages),
			"net.if.total.bytes":    float32(totalBytes),
			"net.if.total.packages": float32(totalPackages),
			"net.if.speed.bits":     float32(speed),
		}
	})

	return nil
}
